//
//  ZXXComposePictureViewCell.swift
//  M-Weibo
//
//  Created by ZXX on 16/4/24.
//  Copyright © 2016年 ZXX. All rights reserved.
//

import UIKit

class ZXXComposePictureViewCell: UICollectionViewCell {
/// 删除的图片回调的闭包
    var composePictureViewCellClosure: (()->())?

///    添加可以为bgImageView 设置图片的属性
    var image: UIImage? {
        didSet{
            
            /*
             -如果image != nil  代表直接使用图片
             -如果image == nil  代表要给他设置加号图片
             compose_pic_add_highlighted
             */
            if image != nil{
                bgImageView.image = image
                // 解决cell复用问题
                bgImageView.highlightedImage = nil
                // 显示删除按钮
                deleteButton.hidden = false
            }else{
                bgImageView.image = UIImage(named: "compose_pic_add")
                // 解决cell复用问题
                bgImageView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
                // 隐藏删除按钮
                deleteButton.hidden = true
            }
        }

    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
//        设置UI
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
//        添加控件
        contentView.addSubview(bgImageView)
        contentView.addSubview(deleteButton)
//        添加约束
        bgImageView.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
            
        }
        deleteButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }

        
    }
    
    // MARK: - 删除按钮监听事件
    @objc private func deleteButtonClick(){
        composePictureViewCellClosure?()
    }
    
    
    
///  背景图片
    private lazy var bgImageView:UIImageView = UIImageView()
    /// 删除按钮
    private lazy var deleteButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(ZXXComposePictureViewCell.deleteButtonClick), forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: "compose_photo_close"), forState: UIControlState.Normal)
        return button
        
    }()
    
}
